package cn.mxz.xmu.robots.basic;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import wang.mouge.common.util.cache.MemcachedClient;

/**
 * 教务机器人工具类，提供一些比较常用的方法
 * 
 * @author kangbin <kangbin@mouge.wang>
 * @author chenmin <chenmin@mouge.wang>
 * @since 2015-03-14
 */
public final class UnivUtils
{
	private static final String[] CN_NUMERIC = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二", "十三", "十四", "十五", "十六" };

	private static final Map<String, String> CN_MAP;

	static
	{
		CN_MAP = new HashMap<String, String>(16);
		for (int i = 0; i < CN_NUMERIC.length; i++)
		{
			CN_MAP.put(CN_NUMERIC[i], (i + 1) + "");
		}
	}

	/**
	 * 将字符串进行URLEncode解密
	 * 
	 * @param input
	 *            解密之前的字符串
	 * @return 解密之后的字符串
	 */
	public static String urlDecode(String input)
	{
		try
		{
			return URLDecoder.decode(input, "UTF-8");

		}
		catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 根据输入的成绩，算出对应的GPA
	 * 
	 * @param score
	 *            输入成绩
	 * @return 计算出的GPA
	 */
	public static String gpa(String score)
	{
		if (score.matches("(\\d+)") || score.matches("(\\d+\\.\\d+)"))
		{
			float numberScore = Float.parseFloat(score);
			if (numberScore >= 90)
				return "4";
			else if (numberScore >= 85)
				return "3.7";
			else if (numberScore >= 81)
				return "3.3";
			else if (numberScore >= 78)
				return "3.0";
			else if (numberScore >= 75)
				return "2.7";
			else if (numberScore >= 72)
				return "2.3";
			else if (numberScore >= 68)
				return "2.0";
			else if (numberScore >= 64)
				return "1.7";
			else if (numberScore >= 60)
				return "1.0";
			else
				return "0";
		}
		else
		{
			if ("A+".equals(score) || "A".equals(score) || "优".equals(score) || "优秀".equals(score))
				return "4";
			else if ("A-".equals(score))
				return "3.7";
			else if ("B+".equals(score))
				return "3.3";
			else if ("B".equals(score) || "良".equals(score) || "良好".equals(score))
				return "3.0";
			else if ("B-".equals(score))
				return "2.7";
			else if ("C+".equals(score))
				return "2.3";
			else if ("C".equals(score))
				return "2.0";
			else if ("C-".equals(score))
				return "1.7";
			else if ("合格".equals(score) || "及格".equals(score))
				return "1.3";
			else if ("D".equals(score))
				return "1.0";
			return "0";
		}

	}

	/**
	 * 将中文的数字转换为阿拉伯数字
	 * 
	 * @param cn
	 *            中文数字
	 * @return 阿拉伯数字
	 */
	public static String cn2numeric(String cn)
	{
		return CN_MAP.get(cn);
	}

	/**
	 * 处理Null异常，判断是否为空
	 * String
	 */
	public static String isNull(String str)
	{
		if (str != null)
		{
			return str.trim();
		}
		return "";
	}

	/**
	 * 获取学期
	 * 
	 * @param term
	 * @return
	 */
	public static String getTerm(String term)
	{
		String returnTerm = "";
		term = RobotsUtlis.regexMatch(term, "([\u6625|\u590f|\u79cb|\u51ac])");
		switch (term)
		{
		case "春":
			returnTerm = "1";
			break;
		case "夏":
			returnTerm = "2";
			break;
		case "秋":
			returnTerm = "0";
			break;
		case "冬":
			returnTerm = "3";
			break;
		}
		return returnTerm;
	}

	/**
	 * @description 判断lesson里的上下午，晚上
	 * @param regex
	 * @param ampm
	 * @return
	 */
	public static boolean filterElements(String ampm, String regex)
	{
		boolean isAmpm = false;
		Pattern regexPat = Pattern.compile(regex);
		Matcher regexMat = regexPat.matcher(ampm);
		if (regexMat.find())
		{
			isAmpm = true;
		}
		return isAmpm;
	}

	public static void showCookie(String sha1)
	{
		System.out.println("cookie:" + MemcachedClient.get("cookie_" + sha1));
	}

	public static void delCookie(String sha1)
	{
		MemcachedClient.delete("cookie_" + sha1);
		MemcachedClient.delete("token_url_" + sha1);
	}

	public static void delClientCookie(String sha1)
	{
		MemcachedClient.delete(sha1);
	}

	/**
	 * 根据当前的时间，返回当前的学期代码，格式如：20141,20150；
	 * 按照规定，春季学期为0，秋季学期为1，暂时不考虑三个学期的情况
	 */
	public static String getCurrentTerm()
	{
		StringBuilder sb = new StringBuilder();
		Calendar calendar = Calendar.getInstance();
		int month = calendar.get(Calendar.MONTH) + 1;
		int year = calendar.get(Calendar.YEAR);

		if (month >= 9)
			sb.append(year).append("0");
		else
			sb.append(year - 1).append("1");

		return sb.toString();
	}

	public static void main(String[] args)
	{
		System.out.println(getCurrentTerm());
	}
}
